package com.kingyea.gradle.jsch;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class JschSession {
	private static final String CHANNEL_TYPE_SFTP = "sftp";
	private static final String CHANNEL_TYPE_EXEC = "exec";
	private Session session;
	
	public JschSession(Session session) {
		this.session = session;
	}
	
	public SftpChannel getSftpChannel() throws JSchException {
		return new SftpChannel((ChannelSftp) session.openChannel(CHANNEL_TYPE_SFTP));
	}
	
	public Channel getChannel(String type) throws JSchException {
		Channel channel = session.openChannel(type);
		return channel;
	}
	
	public ChannelExec getExecChannel() throws JSchException {
		return (ChannelExec) session.openChannel(CHANNEL_TYPE_EXEC);
	}
	
	public int getTimeout() {
		return this.session.getTimeout();
	}
	
	
	public void close() {
		if(this.session.isConnected()) {
			session.disconnect();
		}
	}
	
}
